package com.apac.controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.apac.bom.TestObject;
import com.apac.dao.TestDao;

@Controller
@RequestMapping("/reimbursements")
public class ReimbursementsController {

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView reimbursements(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("Reimbursements");
		
		String reimbursements = generateReimbursements(request, response);
		
		mv.addObject("reimbursements", reimbursements);
		return mv;
	}

	public String generateReimbursements(HttpServletRequest request, HttpServletResponse response){

		
		/**MOCK DATA**/
		ArrayList<TestObject> data = new ArrayList<TestObject>();
		TestDao tDao = new TestDao();
		data = tDao.retrieveAll();
		/**MOCK DATA**/
		
		TableModel tableModel = new TableModel("reimbursements", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(data);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("Reimbursements Record");
			
			Row Row = new Row();
			Table.setRow(Row);
				
			Column col1 = new Column("att1").title("Name");
			Row.addColumn(col1);
	
			Column col2 = new Column("att2").title("Amount");
			Row.addColumn(col2);
	
			Column col3 = new Column("att3").title("Department");
			Row.addColumn(col3); 
	
			Column col4 = new Column("att1").title("Name of Approver");
			Row.addColumn(col4);
	
			Column col5 = new Column("att2").title("Date Credited");
			Row.addColumn(col5);
	
			Column col6 = new Column("att3").title("Classification");
			Row.addColumn(col6); 
	
			Column col7 = new Column("att1").title("Type");
			Row.addColumn(col7);
	
			Column col8 = new Column("att2").title("Description");
			Row.addColumn(col8);
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			return export;
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow HtmlRow = new HtmlRow();
			htmlTable.setRow(HtmlRow);
						
			HtmlColumn col1 = new HtmlColumn("att1").title("Name");
			HtmlRow.addColumn(col1);
	
			HtmlColumn col2 = new HtmlColumn("att2").title("Amount");
			HtmlRow.addColumn(col2);
	
			HtmlColumn col3 = new HtmlColumn("att3").title("Department");
			HtmlRow.addColumn(col3); 
	
			HtmlColumn col4 = new HtmlColumn("att1").title("Name of Approver");
			HtmlRow.addColumn(col4);
	
			HtmlColumn col5 = new HtmlColumn("att2").title("Date Credited");
			HtmlRow.addColumn(col5);
	
			HtmlColumn col6 = new HtmlColumn("att3").title("Classification");
			HtmlRow.addColumn(col6); 

			HtmlColumn col7 = new HtmlColumn("att1").title("Type");
			HtmlRow.addColumn(col7);
	
			HtmlColumn col8 = new HtmlColumn("att2").title("Description");
			HtmlRow.addColumn(col8);
	    	
//	    	HtmlColumn viewLink = new HtmlColumn("id").title("View");
//	    	viewLink.setCellEditor(new CellEditor() {             
//				public Object getValue(Object item, String property, int rowcount) {                 
//	    			//Object value = new HtmlCellEditor().getValue(item, property, rowcount);                 
//	    			HtmlBuilder html = new HtmlBuilder();
//	    			ExamResponse er = (ExamResponse)item;
//	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
//	    			html.append("View Response");
//	    			html.aEnd();
//	    			return html.toString();             
//	    			}         
//	    		});
//	    	viewLink.setSortable(false);
//	    	viewLink.setFilterable(false);
//	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			return html;
		}
	}
}
